﻿Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace DotNetNuke.Modules.ActiveForums
    Partial Public Class af_pollvote
        Inherits ForumBase
        Private PollId As Integer = -1
        Private PollType As String = "S"
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If TopicId > 0 Then
                BindPoll()
            End If
        End Sub
        Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
            Dim stringWriter As System.IO.StringWriter = New System.IO.StringWriter
            Dim htmlWriter As HtmlTextWriter = New HtmlTextWriter(stringWriter)
            MyBase.Render(htmlWriter)
            Dim html As String = stringWriter.ToString()
            html = Utilities.LocalizeControl(html)
            writer.Write(html)
        End Sub
        Private Sub BindPoll()

            Try
                Dim ds As DataSet = DataProvider.Instance.Poll_Get(TopicId)
                If ds.Tables.Count > 0 Then
                    Dim dtPoll As DataTable = ds.Tables(0)
                    Dim dtOptions As DataTable = ds.Tables(1)
                    If dtPoll.Rows.Count > 0 Then
                        lblQuestion.Text = dtPoll.Rows(0).Item("Question").ToString
                        PollType = dtPoll.Rows(0).Item("PollType").ToString
                        PollId = CInt(dtPoll.Rows(0).Item("PollId"))
                        If PollType = "S" Then
                            rdbtnOptions.DataTextField = "OptionName"
                            rdbtnOptions.DataValueField = "PollOptionsID"
                            rdbtnOptions.DataSource = dtOptions
                            rdbtnOptions.DataBind()
                            rdbtnOptions.Visible = True
                            cblstOptions.Visible = False
                        Else
                            cblstOptions.DataTextField = "OptionName"
                            cblstOptions.DataValueField = "PollOptionsID"
                            cblstOptions.DataSource = dtOptions
                            cblstOptions.DataBind()
                            rdbtnOptions.Visible = False
                            cblstOptions.Visible = True
                        End If


                    End If


                End If
            Catch ex As Exception
            End Try
        End Sub

        Private Sub btnVote_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVote.Click
            Try
                Dim optionId As Integer = -1
                If rdbtnOptions.Visible = True Then
                    If rdbtnOptions.SelectedIndex > -1 Then
                        optionId = CInt(rdbtnOptions.SelectedItem.Value)
                    End If
                    If PollId > 0 And optionId > 0 Then
                        DataProvider.Instance.Poll_Vote(PollId, optionId, String.Empty, Request.UserHostAddress, Me.UserId)
                    End If
                ElseIf cblstOptions.Visible = True Then
                    If cblstOptions.SelectedIndex > -1 Then
                        For Each item As ListItem In cblstOptions.Items
                            If item.Selected Then
                                optionId = CInt(item.Value)
                                DataProvider.Instance.Poll_Vote(PollId, optionId, String.Empty, Request.UserHostAddress, Me.UserId)
                            End If
                        Next
                    End If
                End If

                Response.Redirect(Request.RawUrl)

            Catch ex As Exception

            End Try
        End Sub
    End Class
End Namespace
